clear
import delimited "intermediatedata/lichess.csv"

rename clock v12
rename v13 clock


replace clock="" if clock=="None"
destring clock, replace

xtset game turn



drop if timecontrol=="-"
split timecontrol, parse("+") destring force
gen timetaken=l2.clock-clock+timecontrol2

gen timetaken_trim=timetaken
summ timetaken, detail
replace timetaken_trim=. if timetaken>r(p99) | timetaken<r(p1)


egen eval_numbers_only = sieve(eval), char(0123456789.-M)

destring eval_numbers_only, gen(eval2) force ignore("M")
replace eval2=2000*sign(eval2)-eval2 if substr(eval_numbers_only,1,1)=="M"

replace eval2=-eval2 if mod(turn,2)==1

gen start_eval=l.eval2
gen start_clock=l2.clock

gen whitewin=(result=="1-0")
replace whitewin=.5 if (result=="1/2-1/2")

gen opponentclock=l.clock
replace opponentclock=timecontrol1 if turn==1
gen whiteclock=clock*mod(turn,2)+ opponentclock*(1-mod(turn,2))
gen blackclock=clock*(1-mod(turn,2))+ opponentclock*mod(turn,2)


//gen whiteprob=.125+.75/(1+exp(-eval2*.0067))

//gen whiteprob=1/(1+exp(-eval2*.003))

// gen movequality=s.whiteprob
// replace movequality=-movequality if mod(turn,2)==0

gen mover=white
replace mover=black if mod(turn,2)==0

gen moverelo=whiteelo
replace moverelo=blackelo if mod(turn,2)==0

gen opponent=black 
replace opponent=white if mod(turn,2)==0

gen opponentelo=blackelo 
replace opponentelo=whiteelo if mod(turn,2)==0


gen piecetaken=(strpos(move,"x")>0)
bys game (turn): gen piecestaken=sum(piecetaken)

gen piecesonboard=32-piecestaken+piecetaken


egen movercode=group(mover)
egen opponentcode=group(opponent)

bys movercode: egen meanelo=mean(moverelo)
//gen goodplayer=meanelo>1800
//gen badplayer=meanelo<1500


// merge m:1 mover using "intermediatedata/lichess_march_players", gen(newplayer) keep(1 3)
// replace newplayer=(3-newplayer)/2





xtset game turn

save "intermediatedata/lichess", replace

foreach time in 15 30 60 180 300 600 900{

	use "intermediatedata/lichess" if timecontrol=="`time'+0", clear
	save "intermediatedata/lichess`time'", replace
}

//
// preserve
// keep if goodplayer
// xtset game turn
// save "lichess_goodplayers", replace
// restore
//
// preserve
// keep if badplayer
// xtset game turn
// save "lichess_badplayers", replace
// restore
//
// preserve
// keep if newplayer
// xtset game turn
// save "lichess_newplayers", replace
// restore

